package com.empire.module.member.controller.admin.event;

import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import java.util.*;
import java.io.IOException;

import com.empire.framework.common.pojo.PageParam;
import com.empire.framework.common.pojo.PageResult;
import com.empire.framework.common.pojo.CommonResult;
import com.empire.framework.common.util.object.BeanUtils;
import static com.empire.framework.common.pojo.CommonResult.success;

import com.empire.framework.excel.core.util.ExcelUtils;

import com.empire.framework.apilog.core.annotation.ApiAccessLog;
import static com.empire.framework.apilog.core.enums.OperateTypeEnum.*;

import com.empire.module.member.controller.admin.event.vo.*;
import com.empire.module.member.dal.dataobject.event.EventDO;
import com.empire.module.member.service.event.EventService;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

@Tag(name = "管理后台 - 境外人员事件管理")
@RestController
@RequestMapping("/member/event")
@Validated
public class EventController {

    @Resource
    private EventService eventService;

    @PostMapping("/create")
    @Operation(summary = "创建境外人员事件管理")
    @PreAuthorize("@ss.hasPermission('member:event:create')")
    public CommonResult<String> createEvent(@Valid @RequestBody EventSaveReqVO createReqVO) {
        return success(eventService.createEvent(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新境外人员事件管理")
    @PreAuthorize("@ss.hasPermission('member:event:update')")
    public CommonResult<Boolean> updateEvent(@Valid @RequestBody EventSaveReqVO updateReqVO) {
        eventService.updateEvent(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除境外人员事件管理")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('member:event:delete')")
    public CommonResult<Boolean> deleteEvent(@RequestParam("id") String id) {
        eventService.deleteEvent(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得境外人员事件管理")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('member:event:query')")
    public CommonResult<EventRespVO> getEvent(@RequestParam("id") String id) {
        EventDO event = eventService.getEvent(id);
        return success(BeanUtils.toBean(event, EventRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得境外人员事件管理分页")
    @PreAuthorize("@ss.hasPermission('member:event:query')")
    public CommonResult<PageResult<EventRespVO>> getEventPage(@Valid EventPageReqVO pageReqVO) {
        PageResult<EventDO> pageResult = eventService.getEventPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, EventRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出境外人员事件管理 Excel")
    @PreAuthorize("@ss.hasPermission('member:event:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportEventExcel(@Valid EventPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<EventDO> list = eventService.getEventPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "境外人员事件管理.xls", "数据", EventRespVO.class,
                        BeanUtils.toBean(list, EventRespVO.class));
    }

}